// 获取应用实例
const app = getApp()
import env from "../../utils/env"

Page({
  data: {
    userInfo: {
      avatar: '',
      customerId: '',
      depositNum: '',
      phonenumber: '',
      nickName: '',
      waterNum: '',
      thirdSession: '',
    },
    motto: '嗨，好久不见～',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    QRCode: '',
  },
  onLoad() {
  },
  bindGetUserInfo(e) {
    console.log('bindGetUserInfo');
  },
  // 输入昵称
  onInput(e) {
    const { value } = e.detail
    this.setData({
      ['userInfo.nickName']: value
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    this.setData({
      ['userInfo.avatar']: avatarUrl
    })
  },
  getQRCode() {
    if (this.validate()) {
      var third_session = wx.getStorageSync('third_session');
      var Authorization = wx.getStorageSync('access_token');
      this.data.userInfo.thirdSession = third_session;
      var _this = this;
      wx.request({
        url: env.api_host + '/auth/getMyQRCode',
        data: {
          nickName: _this.data.userInfo.nickName,
          avatar: _this.data.userInfo.avatar,
          remark: _this.data.userInfo.thirdSession
        },
        header: {
          Authorization: Authorization,
        },
        method: 'GET',
        success: function (res) {
          var base64Image = "data:image/png;base64," + res.data.data; // 后台返回的base64数据
          var imgData = base64Image.replace(/[\r\n]/g, '') // 将回车换行换为空字符''
          _this.setData({
            QRCode: imgData,
          })
        },
        fail: function (res) {

        },
        complete: function (res) {
        }
      })
    }
  },
  validate() {
    if (this.data.userInfo.nickName != '') {
      if (this.data.userInfo.avatar != '') {
        return true;
      } else {
        wx.showModal({
          title: '温馨提示',
          content: '请上传您的头像照片',
          showCancel: false,
        })
        return false;
      }
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '请输入您的昵称',
        showCancel: false,
      })
      return false;
    }
  },
})